#include "util.h"
#include "threads.h"

int main(int argc, char *argv[])
{

    isAck = 0;
    isNack = 0;
    isLogging = 0;
    wait = 15000;
    prob_erro = 0;
	broadcastPort = 5000;       /* default port */
	char *interface = NULL;
    strcpy(LastMessageinOrder, "");
    
  	message_id = 1;
	last_header.id = 0;
	strcpy(last_header.orig_id, "");
  	start_list(&received_messages);
	int h = 0;
    for (; h < MAXNUMHOSTS; h++){
        strcpy(ackHostsList[h], "EMPTY");
    }
    
    /* Process the comand line arguments */
    char parameter;
    while ((parameter = getopt(argc, argv, "i:p:N:e:AhL")) != -1) {
        switch (parameter) {
			case 'i':
				/*network interface */
				interface = optarg;
				break;
            case 'p':
                /*broadcast port */
                broadcastPort = atoi(optarg);
                break;
            case 'N':
                isNack = 1;
                wait = atoi(optarg);
                break;
            case 'A':
                isAck = 1;
                break;
            case 'e':
                prob_erro = atof(optarg);
                break;
            case 'L':
                isLogging = 1;
                break;
            case 'h':
                printf("./broadcast [OPTION]  [ARGUMENTS]\n");
                printf("      -i <INTERFACE>\n");
                printf("      -p <PORT>\n");
                printf("      -N <value> activate confirmation by nack and set timer with value\n");
                printf("      -A <value> activate confirmation by ack and set timer with value\n");
                printf("      -L <value> activate LOG file\n");                
                printf("\n");
                exit(1);
                break;
            default:
                fprintf(stderr,"Usage:  %s -p <Port> . Use -h for help.\n", argv[0]);
                exit(1);
        }
    }

	if (interface == NULL) interface = "eth0"; /* default network interface */




    /*  broadcast IP address */ 
    if (getBroadcastAddress(interface, broadcastIP)) return 1;
    if (getIPAddress(interface, IPAddress)) return 1;
    printf("IP Address: %s\n", IPAddress);
    printf("Broadcast IP: %s\n", broadcastIP);
    

	/* thread to receiving messages */
    thread_start_receive ();

    LogFile("\n\n=======================================================\n");
    LogFile("Starting ...\n");
    //LogFile(strncat("IP Address: ", IPAddress, 16));
    //fprintf(LOGFile, "Broadcast IP: %s\n", broadcastIP);
    
    printf("Starting ...\n"); /* sending messages */

    char msg[255];
    while (1){
	    /* wait for messages */
	    scanf("%s", msg);
	    
	    Header hdr;
	    char ack_type;
	    if (isAck) ack_type = 'A';
	    else ack_type = 'N';
	    initialize_header (&hdr, message_id, ack_type, MSG, IPAddress, broadcastIP);
	    message_id++;

	    char* block;
	    int sendMsgLen = 0;
	     
	    if (strcmp(msg, CLOSE_MSG))
	        sendMsgLen = encapsula(&block, msg, &hdr);
	    else{
	        hdr.type = CLOSE;
	        sendMsgLen = encapsula(&block, msg, &hdr);
	    }

        printf("Sending %s\n", msg);
	    send_broadcast_msg(block, sendMsgLen);	
	    
    }
    
    
	
	return 0;

}


